/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.intec.matsab.form;

import edu.intec.matsab.dao.AsignaturaDAO;
import edu.intec.matsab.dao.SeccionDAO;
import edu.intec.matsab.persistence.Asignatura;
import edu.intec.matsab.persistence.Clase;
import edu.intec.matsab.persistence.ClasePK;
import edu.intec.matsab.persistence.Seccion;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.el.ELContext;
import javax.el.ValueExpression;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author matilde
 */
public class ManejoSeccionBean {

    private Seccion seccion;
    private List<Asignatura> asignaturaItems;
    private Asignatura selectedAsignatura;
    private List<Clase> clasesList;
    private List<SelectItem> diasItems;
    private List<SelectItem> horasItems;

    public ManejoSeccionBean() {
        clasesList = new ArrayList<Clase>();
        seccion = new Seccion();
        selectedAsignatura = new Asignatura();
        asignaturaItems = AsignaturaDAO.getAllAsignaturas();

        createSelectItems();
    }

    public Seccion getSeccion() {
        return seccion;
    }

    public void setSeccion(Seccion seccion) {
        this.seccion = seccion;
    }

    public List<Asignatura> getAsignaturaItems() {
        return asignaturaItems;
    }

    public void setAsignaturaItems(List<Asignatura> asignaturaItems) {
        this.asignaturaItems = asignaturaItems;
    }

    public Asignatura getSelectedAsignatura() {
        return selectedAsignatura;
    }

    public void setSelectedAsignatura(Asignatura selectedAsignatura) {
        this.selectedAsignatura = selectedAsignatura;
    }

    public List<Clase> getClasesList() {
        return clasesList;
    }

    public void setClasesList(List<Clase> clasesList) {
        this.clasesList = clasesList;
    }

    public List<SelectItem> getDiasItems() {
        return diasItems;
    }

    public void setDiasItems(List<SelectItem> diasItems) {
        this.diasItems = diasItems;
    }

    public List<SelectItem> getHorasItems() {
        return horasItems;
    }

    public void setHorasItems(List<SelectItem> horasItems) {
        this.horasItems = horasItems;
    }

    public String updateAsignatura() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String row = (String) request.getParameter("idAsignatura");

        selectedAsignatura = new Asignatura();
        int id = 0;

        try {
            id = Integer.parseInt(row);

        } catch (NumberFormatException numberFormatException) {
            numberFormatException.printStackTrace();
        }

        selectedAsignatura = asignaturaItems.get(id);

        return "";
    }

    /* Se envia un objeto seccion a la clase SeccionDAO
     * para agregar una nueva seccion
     * @see SeccionDAO
     * */
    public String insertar() {

        SeccionDAO.insertar(seccion);

        return "";
    }

    public void agregarClase(ActionEvent event) {
        Clase c = new Clase();
        c.setClasePK(new ClasePK());
        clasesList.add(c);

        FacesContext.getCurrentInstance().renderResponse();
    }

    public void eliminarClase(ActionEvent event) {
        Iterator itr = clasesList.iterator();

        while (itr.hasNext()) {
            Clase c = (Clase) itr.next();

            if (c.isSelected()) {
                itr.remove();
            }
        }

        FacesContext.getCurrentInstance().renderResponse();
    }

    private void createSelectItems() {
        String[] dias = {"Lunes", "Martes", "Miercoles", "Jueves", "Viernes", "Sabado"};
        diasItems = new ArrayList<SelectItem>();

        for (int i = 0; i < dias.length; i++) {
            diasItems.add(new SelectItem(new Integer(i + 1), dias[i]));
        }

        horasItems = new ArrayList<SelectItem>();

        for (int i = 1; i <= 10; i++) {
            String hora = String.valueOf(i) + ":00";
            horasItems.add(new SelectItem(new Integer(i), hora));
        }
    }

    public void resetearBean(ActionEvent event) {
        ELContext elContext = FacesContext.getCurrentInstance().getELContext();
        ValueExpression ve = FacesContext.getCurrentInstance().getApplication().getExpressionFactory().createValueExpression(elContext, "#{seccionBean}", ManejoSeccionBean.class);

        ManejoSeccionBean bean = new ManejoSeccionBean();
        ve.setValue(elContext, bean);
    }
    
    
}

